Textcsv::App.controllers :cust_groups do

  # get :index, :map => '/foo/bar' do
  #   session[:foo] = 'bar'
  #   render 'index'
  # end

  # get :sample, :map => '/sample/url', :provides => [:any, :js] do
  #   case content_type
  #     when :js then ...
  #     else ...
  # end

  # get :foo, :with => :id do
  #   "Maps to url '/foo/#{params[:id]}'"
  # end

  # get '/example' do
  #   'Hello world!'
  # end

  get :index do
    render "cust_groups/index"

  end

  get :data do

    @sort = "desc";
    if params[:sort]
      @sort = params[:sort]
    else
      @sort = "desc";
    end
    @cust_groups = CustGroup.skip(params[:offset]).limit(params[:limit]).order_by(created_at: "#{@sort}")
    @result = {
      "total": 800,
       "rows": @cust_groups
    }
    @result.to_json


  end

  post :create, :provides => :js  do
    if params[:group_name].blank?
        "alert('用户组名不得为空');"

    else
      @cust_group = CustGroup.new
      @cust_group.name = params[:group_name]
      if @cust_group.save
        "afterSubmit();console.log('afterSubmit');"
      else
        "alert('保存未成功，请重试！');"
      end

    end

  end

  get :delete, :with => :id do
    if CustGroup.find(params[:id]).destroy
      1.to_s
    else
      0.to_s
    end
  end

  get :group_users, :with => :id do
    CustGroup.find(params[:id]).didi_cust_phones.to_json
  end

  get :group_users_count, :with => :id do
    CustGroup.find(params[:id]).didi_cust_phones.count.to_s
  end

end
